<?php namespace App\Http\Controllers;

use App\Answers;
use App\Banner;
use App\DakaContent;
use App\Http\Services\FileManager;
use App\Page;
use App\Plan;
use App\PlanJoin;
use App\Profession;
use App\Questions;
use App\SysFeedback;
use App\Classify;
use App\Product;
use App\ProductImgs;
use App\User;
use App\UserInfo;
use App\VisitLog;
use App\Admin;
use App\HomeImg;
use Illuminate\Contracts\Auth\Guard;
use Auth;
use DB;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function login(){
        return view('admin.login');
    }

    public function homeImgList(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录']);

        $homeImg = HomeImg::orderBy('created_at','desc')->get();

        if(count($homeImg) > 0){
            foreach ($homeImg as $home){
                $home->img_url = FileManager::getImgFromOssUrl($home->img_id);
            }
        }

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.home_img-list',['homeImgs' => $homeImg,'isAdmin' => $isAdmin]);
    }

    public function homeImgSet(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $sysHomeImg = HomeImg::whereId($id)->first();

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.home_img-set',['homeImg'=> $sysHomeImg,'isAdmin' => $isAdmin]);
    }

    public function userList(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $users = User::orderBy("created_at","desc")->paginate(50);

        foreach($users as $user){
            /** @var User $user */
            $user->userInfo;
        }

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.user-list',["users"=>$users,'isAdmin' => $isAdmin]);
    }

    public function bannerList(){

        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $sysBanners = Banner::orderBy('sort','asc')->orderBy('created_at','desc')->get();

        if(count($sysBanners) > 0){
            foreach ($sysBanners as $banner){
                $banner->img_url = FileManager::getImgFromOssUrl($banner->img_id);
            }
        }

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.banner-list',['banners'=>$sysBanners,'isAdmin' => $isAdmin]);
    }

    public function bannerSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBanner = Banner::whereId($id)->first();

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.banner-set',['banner'=>$thatBanner,'isAdmin' => $isAdmin]);
    }

    public function productList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $hhProduct = Product::orderBy("created_at","desc")->paginate(50);
        if($hhProduct){
            foreach($hhProduct as $product){
                $one= Classify::whereId($product->classify_one)->first();
                $two= classify::whereId($product->classify_two)->first();
                $three= classify::whereId($product->classify_three)->first();

                $product->classify_one = $one ? $one->name : "无";
                $product->classify_two = $two ? $two->name : "无";
                $product->classify_three = $three ? $three->name : "无";
            }
        }
        if(count($hhProduct) > 0){
            foreach ($hhProduct as $product){
                $product->video_url = FileManager::getVideoFromOssUrl($product->video_id);
            }
        }

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.product-list',['products' => $hhProduct,'isAdmin' => $isAdmin]);
    }

    public function productSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $classify_one = Classify::whereType('part_one')->get();
        $classify_two = Classify::whereType('part_two')->get();
        $classify_three = Classify::whereType('part_three')->get();

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $sysProduct = Product::whereId($id)->first();

        if($sysProduct){
            $productImgs = ProductImgs::wherePId($sysProduct->id)->get();

            $img_strs = "";
            $img_urls = array();
            if(count($productImgs) > 0){
                foreach ($productImgs as $productImg){
                    /** @var ProductImgs $productImg */
                    $imgUrl = FileManager::getAliOssFile($productImg->img_id);
                    array_push($img_urls, $imgUrl);
                    if($img_strs == ""){
                        $img_strs = $productImg->img_id;
                    }else{
                        $img_strs = $img_strs . "," . $productImg->img_id;
                    }
                }
            }

            $sysProduct->img_strs = $img_strs;
            $sysProduct->img_urls = $img_urls;
        }

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();
        return view('admin.product-set',['classOne'=>$classify_one,'classTwo'=>$classify_two,'classThree'=>$classify_three,'sysProduct' => $sysProduct,'isAdmin' => $isAdmin]);
    }

    public function classifyList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $firstClassifys = Classify::wherePid(0)->orderBy('created_at','desc')->get();

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.classify-list',['classifys'=>$firstClassifys,'isAdmin' => $isAdmin]);
    }

    public function productImgsList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $productId = $request->get('id');

        if($productId){
            $firstProductImgs = ProductImgs::where('p_id',$productId)->orderBy('created_at')->paginate(50);

        }else{
            $firstProductImgs = ProductImgs::orderBy('p_id','asc')->orderBy('created_at','desc')->paginate(50);

        }

        if(count($firstProductImgs) > 0){
            foreach ($firstProductImgs as $productImgs){
                $productImgs->img_url = FileManager::getImgFromOssUrl($productImgs->img_id);
            }
        }

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.product_imgs-list',['productImgs'=>$firstProductImgs,'isAdmin' => $isAdmin]);
    }

    public function productImgsSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatProductImgs = ProductImgs::whereId($id)->first();

        $nowproduct = Product :: get();

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        return view('admin.product_imgs-set',['productImgs' => $thatProductImgs,'product' => $nowproduct,'isAdmin'=>$isAdmin]);
    }

    public function adminList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();

        $newAdmin = Admin::orderBy('created_at','desc')->paginate(10);
        return view('admin.admin-list',['admins' => $newAdmin,'isAdmin' => $isAdmin ]);
    }

    public function adminSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();
        return view('admin.admin-set',['isAdmin' => $isAdmin]);
    }
    public function changePwd(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $user = Auth::guard('admin')->user();

        $pp = $user->username;

        $isAdmin = Admin::where('username',$pp)->first();
        return view('admin.change-psw',['isAdmin' => $isAdmin]);
    }
}